﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace UI.UserControls
{
    public partial class MFAPasswordBox : UserControl
    {
        public MFAPasswordBox()
        {
            InitializeComponent();
            exColor = ((SolidColorBrush)base.Foreground).Color;
            PasswordBox1.LostFocus += new RoutedEventHandler(PasswordBox1_LostFocus);
            PasswordBox1.GotFocus += new RoutedEventHandler(PasswordBox1_GotFocus);
            PasswordBox1.PasswordChanged += new RoutedEventHandler(PasswordBox1_PasswordChanged);
        }

        void PasswordBox1_PasswordChanged(object sender, RoutedEventArgs e)
        {
            if (PasswordBox1.Password != BackGroundText)
            {
                Password = PasswordBox1.Password;
            }
        }

        void PasswordBox1_LostFocus(object sender, RoutedEventArgs e)
        {
            if (PasswordBox1.Password.Trim() == "")
            {
                PasswordBox1.Password = BackGroundText;
                exColor = ((SolidColorBrush)base.Foreground).Color;
                PasswordBox1.Foreground = new SolidColorBrush(Colors.Gray);
            }
            else
            {
                PasswordBox1.Foreground = new SolidColorBrush(exColor);
            }
        }

        void PasswordBox1_GotFocus(object sender, RoutedEventArgs e)
        {
            if (PasswordBox1.Password == BackGroundText)
            {
                PasswordBox1.Password = "";
            }
            PasswordBox1.Foreground = new SolidColorBrush(exColor);
        }

        private string _BackGroundText;
        public string BackGroundText
        {
            get { return _BackGroundText; }
            set
            {
                _BackGroundText = value;
                PasswordBox1.Password = value;
                PasswordBox1.Foreground = new SolidColorBrush(Colors.Gray);
            }
        }

        private Color exColor;

        public string Password
        {
            get
            {
                if ((string)GetValue(PasswordProperty) == BackGroundText)
                {
                    return "";
                }
                return (string)GetValue(PasswordProperty);
            }
            set
            {
                if (value == Password)
                {
                    return;
                }

                if (value.Trim() == "")
                {
                    SetValue(PasswordProperty, BackGroundText);
                    PasswordBox1.Password = BackGroundText;
                }
                else
                {
                    SetValue(PasswordProperty, value);
                    PasswordBox1.Password = value;
                }

            }
        }

        // Using a DependencyProperty as the backing store for Password.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PasswordProperty =
            DependencyProperty.Register("Password", typeof(string), typeof(MFAPasswordBox), new PropertyMetadata(null, onPasswordChanged));



        public static void onPasswordChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {

        }

    }
}
